package com.sunwayhorizo.health.platform.base;

import java.util.List;

import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.Logger;
import org.jboss.seam.log.Log;

import com.sunwayhorizo.health.business.dto.ResultsDTO;
import com.sunwayhorizo.health.common.ListUtil;
import com.sunwayhorizo.health.platform.common.PlatformConstant;

public class BaseManager{
	
	/**
	 * Seam内置Log
	 */
	@Logger
	protected Log log;
	
	@PersistenceContext(unitName="sunwayhealthEts")
	protected EntityManager entityManager2;
	
	/**
	 * 批量操作的基数（默认50）
	 */
	public static final int DEFAULT_BATCH_NUMBER = 50;
	
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public <T> T find(Class <T> entityClass, Object primaryKey){
       T obj = entityManager2.find(entityClass, primaryKey);
       return obj;
    }

    public void save(Object entity){
    	entityManager2.persist(entity);
    	flushClear(entityManager2);
    }
    
	@SuppressWarnings("rawtypes")
	public void save(List list,int maxBatchNumber){
    	for(int i=0;ListUtil.isNotEmpty(list)&& i<list.size();i++){
    		entityManager2.persist(list.get(i));
    		if( i% maxBatchNumber == 0){
    			flushClear(entityManager2);
    		}
    	}
    	flushClear(entityManager2);
    }
    
	@SuppressWarnings("rawtypes")
	public void save(List list){
    	save(list,DEFAULT_BATCH_NUMBER);
    }

    public void update(Object entity){
    	entityManager2.merge(entity);
    	flushClear(entityManager2);
    }
    
	@SuppressWarnings("rawtypes")
	public void update(List list,int maxBatchNumber){
    	for(int i=0;ListUtil.isNotEmpty(list)&&i<list.size();i++){
    		entityManager2.merge(list.get(i));
    		if( i% maxBatchNumber == 0){
    			flushClear(entityManager2);
    		}
    	}
    	flushClear(entityManager2);
    }
    
	@SuppressWarnings("rawtypes")
	public void update(List list){
    	update(list,DEFAULT_BATCH_NUMBER);
    }
    
	public void delete(Class<?> entityClass, Object primaryKey){
		entityManager2.remove(entityManager2.merge(entityManager2.getReference(entityClass, primaryKey)));
		flushClear(entityManager2);
    }
	
	@SuppressWarnings("rawtypes")
	public void delete(Class<?> entityClass, List primaryKeyList,int maxBatchNumber){
		for(int i=0;ListUtil.isNotEmpty(primaryKeyList)&&i<primaryKeyList.size();i++){
			entityManager2.remove(entityManager2.merge(entityManager2.getReference(entityClass, primaryKeyList.get(i))));
    		if( i% maxBatchNumber == 0){
    			flushClear(entityManager2);
    		}
    	}
		flushClear(entityManager2);
    }
	
	@SuppressWarnings("rawtypes")
	public void delete(Class<?> entityClass, List primaryKeyList){
		delete(entityClass,primaryKeyList,DEFAULT_BATCH_NUMBER);
    }
	
	
    public void delete(Object entity){
    	entityManager2.remove(entityManager2.merge(entity));
    	flushClear(entityManager2);
    }
    
    @SuppressWarnings("rawtypes")
	public void delete(List list,int maxBatchNumber){
    	for(int i=0;ListUtil.isNotEmpty(list)&&i<list.size();i++){
    		entityManager2.remove(entityManager2.merge(list.get(i)));
    		if( i% maxBatchNumber == 0 ){
    			flushClear(entityManager2);
    		}
    	}
    	flushClear(entityManager2);
    }
    
    @SuppressWarnings("rawtypes")
	public void delete(List list){
    	delete(list,DEFAULT_BATCH_NUMBER);
    }
    
    public void flushClear(EntityManager entityManager){
    	if( entityManager != null ){
    		entityManager.flush();
    		entityManager.clear();
    	}
    }
    
    public ResultsDTO getResultsDTO(String resultDesc, boolean resultFlag, Object object){
		ResultsDTO dto = new ResultsDTO();
		dto.setResultDesc(resultDesc);
		dto.setResultFlag(resultFlag);
		dto.setObject(object);
		return dto;
	}
    
    public ResultsDTO getResultsDTO(int currentPage,int pageSize,String resultDesc, boolean resultFlag, Object object){
		ResultsDTO dto = new ResultsDTO();
		dto.setToPage(currentPage);
		dto.setPageSize(pageSize);
		dto.setResultDesc(resultDesc);
		dto.setResultFlag(resultFlag);
		dto.setObject(object);
		return dto;
	}
    
    /**
     * 判断是否启用分页
     * @param startPosition 开始位置
     * @param pageSize 一页显示多少数据
     * @return 若开始位置和一页显示的数据条数都是-1，即认为不需要分页，不然则认为需要分页
     */
    public boolean hasPagination(int startPosition,int pageSize){
    	if( PlatformConstant.ALL_POS == startPosition && PlatformConstant.ALL_POS == pageSize ){
    		return false;
    	}else{
    		return true;
    	}
    }
}